function [vfp, stim_time] = FP_artsupp(volt, dfile, sf, autoflag, artifact_threshold, st_list)
%
% field potential artifact suppressor.
% uses stim times and "deadwin" to suppress artifcat in data set prior to
% filtering and analysis...
%
% 12/10/01 Paul B. Manis.
%
% modified to find automatically if flag set...
%

global CONTROL
use_stimlist = 0; 
if(sf <= 0)
    return;
end
vfp =  volt;
if(nargin < 4)
    use_stimlist = 0;
else
    use_stimlist = 1;
end;

if(nargin < 5)
    artlevel = 0; % use default
else
    artlevel = artifact_threshold;
end;

if(nargin < 6) % grab a stim list from the 6th arg in the call.
    use_stimlist2 = 0;
    
else
    use_stimlist2 = 1;
end;

if(use_stimlist == 0)
    stim_list=CONTROL(sf).stim_time; % get the array
    % psp_time=CONTROL(sf).psp_time; % get psp definition array
    % if(ischar(psp_time))
    %    psp_time = str2num(psp_time);
    % end;
    if(ischar(stim_list))
        stim_time= str2num(stim_list);
    else
        stim_time = stim_list;
    end;
else
    stim_time = find_artifacts(vfp, dfile, 1, artlevel); % get from first record in series.
%    length(stim_time)
end
if(iscell(stim_time))
    stim_time = stim_time{1};
end;

if(use_stimlist2 == 1)
    stim_time = st_list; % use this instead!!!
end;

time=make_time(dfile);
RATES = (dfile.rate .* dfile.nr_channel)/ 1000; % array of sampling rates, convert to msec
if(ischar(CONTROL(sf).deadwin))
    deadwin = str2num(CONTROL(sf).deadwin);
else
    deadwin = CONTROL(sf).deadwin;
end;
deadwin =    floor((deadwin./RATES)+0.5);
ts1 = zeros(length(stim_time), size(vfp,1));
ts2 = ts1;
for j = 1:length(stim_time)
    ts1(j,:) = floor((stim_time(j)./RATES)+0.5);
    ts2(j,:) = ts1(j,:)+deadwin;
end;

for i = 1:size(vfp,1)
    for j = 1:length(stim_time)
         vfp(i,[ts1(j,i):ts2(j,i)]) = NaN * zeros(1, length((ts1(j,i):ts2(j,i))));
       % vfp(i,[ts1(j,i):ts2(j,i)]) = mean(vfp(i,[(ts1(j,i)-5):(ts1(j,i)-2)]));
    end;
end;
if(nargout == 0) % plot suppressed data... 
    hf = newfigure('artsupp', 'Artifact Suppressed Traces');
    for i = 1: size(vfp, 1)
        plot(time(i,:), vfp(i,:))
        hold on;
    end;
end;
